import psutil
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView


class ServerView(APIView):
    permission_classes = [IsAuthenticated]

    def get(self, request, *args, **kwargs):
        data = {'cpu': {}, 'memory': {}, 'disk': {}}
        data['cpu']['count'] = psutil.cpu_count()
        data['cpu']['lcount'] = psutil.cpu_count(logical=False)
        data['cpu']['percent'] = psutil.cpu_percent(interval=1)
        memory = psutil.virtual_memory()
        data['memory']['total'] = round(memory.total / 1024 / 1024 / 1024, 2)
        data['memory']['used'] = round(memory.used / 1024 / 1024 / 1024, 2)
        data['memory']['percent'] = memory.percent
        disk = psutil.disk_usage('/')
        data['disk']['total'] = round(disk.total / 1024 / 1024 / 1024, 2)
        data['disk']['used'] = round(disk.used / 1024 / 1024 / 1024, 2)
        data['disk']['percent'] = disk.percent
        return Response(data)
